﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using PRET.ReportDataDefine.UIData;
using PRET.ReportControlLib.ServiceReference;

namespace PRET.ReportControlLib.ReportView
{
    public partial class ProductionReportView : UserControl
    {
        private List<OrderCheckInfo> orderList = new List<OrderCheckInfo>();
        private PRETWebServiceClient adapter = null;
        private List<Microsoft.Reporting.WinForms.ReportViewer> reportViewList = new List<Microsoft.Reporting.WinForms.ReportViewer>();
        private List<TabPage> tabPageList = new List<TabPage>();

        public ProductionReportView()
        {
            try
            {
                InitializeComponent();
                this.adapter = new PRETWebServiceClient();
                this.checkTimePanel.correctTimeEvent += checkTimePanel_correctTimeEvent;
                this.checkTimePanel.ignoreTimeEvent += checkTimePanel_ignoreTimeEvent;
                this.checkTimePanel.showReportEvent += checkTimePanel_showReportEvent;
            }
            catch(Exception ex)
            {
                string mess = ex.Message;
            }
        }

        void checkTimePanel_showReportEvent(object sender, Controls.ShowReportViewEventArgs args)
        {
            try
            {
                this.checkTimePanel.EnableControls(false);
                OrderCheckInfo order = args.Order;
                if (order == null)
                    return;

                if (this.adapter == null)
                {
                    MessageBox.Show("ProductionReportView 没有初始化Server Adapter!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                // 清空已有的数据
                foreach (Microsoft.Reporting.WinForms.ReportViewer item in this.reportViewList)
                    item.LocalReport.DataSources.Clear(); //clear report
                this.reportTabControl.Controls.Clear();

                // 从后台获取生产报表数据
                //ProductionReportSummaryForJSON[] json = this.adapter.GetProLineStatus();
                ProductionReport[] dataList = this.adapter.GetProductionReport(order);
                if (dataList == null)
                {
                    string message = string.Format("读取订单{0}发生错误!", order.OrderID);
                    MessageBox.Show(message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                for (int i = 0; i < dataList.Length; i++)
                {
                    if (dataList[i] != null)
                    {
                        if (i >= tabPageList.Count)
                        {
                            NewReportViewTabPage(i + 1);
                        }
                        Microsoft.Reporting.WinForms.ReportDataSource summaryDataset = new Microsoft.Reporting.WinForms.ReportDataSource("ProductionReportSummaryDS"); // set the datasource
                        this.reportViewList[i].LocalReport.DataSources.Add(summaryDataset);
                        summaryDataset.Value = new List<ProductionReportSummary> { dataList[i].Summary };

                        Microsoft.Reporting.WinForms.ReportDataSource detailsDataset = new Microsoft.Reporting.WinForms.ReportDataSource("ProductionReportDetailInfoDS");
                        this.reportViewList[i].LocalReport.DataSources.Add(detailsDataset);
                        if (dataList[i].DataList != null)
                            detailsDataset.Value = dataList[i].DataList;

                        this.reportTabControl.Controls.Add(this.tabPageList[i]);
                        this.reportViewList[i].LocalReport.Refresh();
                        this.reportViewList[i].RefreshReport();
                    }
                    else
                    {
                        string message = string.Format("系统数据库中没有订单'{0}'的记录!", order.OrderID);
                        MessageBox.Show(message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = string.Format("ProductionReportView.checkTimePanel_showReportEvent() Failed: {0}.\r\nDetail: {1}", ex.Message, ex.StackTrace);
                MessageBox.Show(msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            { this.checkTimePanel.EnableControls(true); }
        }

        void checkTimePanel_correctTimeEvent(object sender, ReportControlLib.Controls.CorrectOrderTimeEventArgs e)
        {
            try
            {
                this.checkTimePanel.EnableControls(false);
                this.adapter.CorrectJCJOrderTime(e.Order);
            }
            catch (Exception)
            {
                throw new NotImplementedException();
            }
            finally
            { this.checkTimePanel.EnableControls(true); }
        }
        void checkTimePanel_ignoreTimeEvent(object sender, ReportControlLib.Controls.CorrectOrderTimeEventArgs e)
        {
            try
            {
                this.checkTimePanel.EnableControls(false);
                this.adapter.IgnoreERPOrderTime(e.Order);
            }
            catch (Exception)
            {
                throw new NotImplementedException();
            }
            finally
            { this.checkTimePanel.EnableControls(true); }
        }

        public void SetOrderList()
        {
            try
            {
                OrderCheckInfo[] list = this.adapter.GetJCJOrderList();
                if (list != null && list.Length > 0)
                    this.checkTimePanel.InitializeOrderIDPanel(list.ToList<OrderCheckInfo>());
            }
            catch(Exception ex)
            {
                string msg = ex.Message;
            }
        }
        
        private void NewReportViewTabPage(int index)
        {
            // 初始化ReportView
            Microsoft.Reporting.WinForms.ReportViewer productionReportViewer = new Microsoft.Reporting.WinForms.ReportViewer();
            productionReportViewer.AutoSize = true;
            productionReportViewer.Dock = System.Windows.Forms.DockStyle.Fill;
            Microsoft.Reporting.WinForms.ReportDataSource reportDataSource = new Microsoft.Reporting.WinForms.ReportDataSource();
            reportDataSource.Name = "ProductionReportDS";
            reportDataSource.Value = null;
            productionReportViewer.LocalReport.DataSources.Add(reportDataSource);
            productionReportViewer.LocalReport.ReportEmbeddedResource = "PRET.ReportControlLib.RDLC.ProductionReport.rdlc";
            productionReportViewer.Location = new System.Drawing.Point(3, 3);
            productionReportViewer.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
            productionReportViewer.Name = "productionReportViewer" + index.ToString();
            productionReportViewer.Size = new System.Drawing.Size(1005, 200);
            reportViewList.Add(productionReportViewer);
            // 初始化TabPage
            TabPage tabPage = new TabPage();
            tabPage.SuspendLayout();
            tabPage.Controls.Add(productionReportViewer);
            tabPage.Location = new System.Drawing.Point(4, 4);
            tabPage.Name = "tabPage" + index.ToString();
            tabPage.Padding = new System.Windows.Forms.Padding(3);
            tabPage.Size = new System.Drawing.Size(1011, 206);
            tabPage.Text = "生产报表" + index.ToString();
            tabPage.UseVisualStyleBackColor = true;
            tabPage.ResumeLayout(false);
            tabPage.PerformLayout();
            tabPageList.Add(tabPage);
        }
    }
}
